package me.itsoo.elasticbatis.binding;

import lombok.Getter;
import me.itsoo.elasticbatis.mapping.MappedStatement;
import me.itsoo.elasticbatis.mapping.SqlCommandType;
import me.itsoo.elasticbatis.session.Configuration;
import me.itsoo.elasticbatis.session.SqlSession;

import java.lang.reflect.Method;

/**
 * MapperMethod
 *
 * @author zhangxy4
 * @version 0.0.1
 * @since 2025/4/4
 */
public class MapperMethod {

    private final SqlCommand command;

    public MapperMethod(Class<?> mapperInterface, Method method, Configuration configuration) {
        this.command = new SqlCommand(configuration, mapperInterface, method);
    }

    /**
     * 执行SQL
     *
     * @param sqlSession SqlSession
     * @param args       args
     * @return data
     */
    public Object execute(SqlSession sqlSession, Object[] args) {
        Object result = null;
        switch (command.getType()) {
            case INSERT:
                // TODO:
                break;
            case DELETE:
                // TODO:
                break;
            case UPDATE:
                // TODO:
                break;
            case SELECT:
                result = sqlSession.selectOne(command.getName(), args);
                break;
            default:
                throw new RuntimeException("Unknown execution method for: " + command.getName());
        }
        return result;
    }


    /**
     * SqlCommand
     */
    @Getter
    public static class SqlCommand {

        private final String name;
        private final SqlCommandType type;

        public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
            String statementName = mapperInterface.getName() + "." + method.getName();
            MappedStatement ms = configuration.getMappedStatement(statementName);
            this.name = ms.getId();
            this.type = ms.getSqlCommandType();
        }
    }
}
